/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.tools;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 *
 * @author root
 */
public class XmlTools {

    //输入xml和key <parameter name="hdfsPath">http://10.128.11.11:9000</parameter>
    public static String getValueFromStrDGText = "";//final value for key

    public static synchronized String getValueFromStrDGText(String xml, String name_key) {
        getValueFromStrDGText = "";
        Document doc = null;
        SAXReader reader = new SAXReader();
        try {
            //String code = xmlPath.get
            InputStream in = new ByteArrayInputStream(xml.getBytes("utf-8"));
            doc = reader.read(in);
        } catch (Exception ex) {
            return "";
        }
        if (doc != null) {
            Element rootEl = doc.getRootElement();
            getElValueDGText(rootEl, name_key);

            if (getValueFromStrDGText != null && !getValueFromStrDGText.equals("")) {
                return getValueFromStrDGText;
            }
        }
        return "";
    }

    private static synchronized void getElValueDGText(Element rootEl, String key) {
        if (rootEl.elements().size() > 0) {
            List<Element> childrenList = rootEl.elements();
            for (int i = 0; i < childrenList.size(); i++) {
                getElValueDGText(childrenList.get(i), key);
            }
        }
        String _name = rootEl.attributeValue("name");
        //logger.info("xml'name: " + _name);
        if (_name != null && _name.equals(key)) {
            if (!getValueFromStrDGText.equals("")) {
                getValueFromStrDGText = getValueFromStrDGText + "|" + rootEl.getText();
            } else {
                getValueFromStrDGText = rootEl.getText();
            }
        }
    }

    //读subjobid <subjobid>9NRXLXcfSEehU2ecMKbm9A</subjobid>
    public static synchronized String getElValueFromStr(String xml, String key) {
        Document doc = null;
        SAXReader reader = new SAXReader();
        try {
            //String code = xmlPath.get
            InputStream in = new ByteArrayInputStream(xml.getBytes("utf-8"));
            doc = reader.read(in);
        } catch (Exception ex) {
            return "";
        }
        if (doc != null) {
            Element rootEl = doc.getRootElement();
            Element el = rootEl.element(key);
            String str = "";
            str = el.getText();
            return str;
        }
        return "";
    }

    //添加节点，在subjobid后面添加
    public static synchronized String addEl(String xml, String subjobid, String key, String value) {
        Document doc = null;
        SAXReader reader = new SAXReader();
        try {
            //String code = xmlPath.get
            InputStream in = new ByteArrayInputStream(xml.getBytes("utf-8"));
            doc = reader.read(in);
        } catch (Exception ex) {
            return "";
        }
        if (doc != null) {
            Element rootEl = doc.getRootElement();
            Element el = rootEl.element(subjobid);
            Element elp = el.getParent();
            Element tmp = elp.addElement(key);
            tmp.setText(value);
            return rootEl.asXML();
        }
        return "";
    }

    //获取HDFSPath
    /**
     * 获取3级节点的name属性值为“attribute”的value
     *
     * @param xml 原xml
     * @param element1 1子节点
     * @param element2 2子节点
     * @param element3 3子节点
     * @param attribute 子节点属性名
     * @return value 属性值
     */
    public static synchronized String getElement3AttributeValue(String xml, String element1, String element2, String element3, String attribute) {
        Document doc = null;
        String value = "";
        SAXReader reader = new SAXReader();
        try {
            InputStream in = new ByteArrayInputStream(xml.getBytes("utf-8"));
            doc = reader.read(in);
        } catch (Exception ex) {
            return "";
        }
        if (doc != null) {
            Element rootEl = doc.getRootElement();
            Element ele1 = rootEl.element(element1);
            Element ele2 = ele1.element(element2);

            List<Element> ele3 = ele2.elements();
            for (Element ele : ele3) {
                if (ele.getName().equals(element3)) {
                    if (ele.attributeValue("name").equals(attribute)) {
                        value = ele.attributeValue("value");
                        break;
                    }
                }
            }
        }
        return value;
    }

    public static synchronized ArrayList<HashMap<String, String>> getIterationElementAttributeValue(String xml, String element1, String element2, String element3, String element4) throws DocumentException {
        String ip = "";
        String port = "";
        String username = "";
        String password = "";
        String filepath = "";
        ArrayList<HashMap<String, String>> ftpList = new ArrayList<HashMap<String, String>>();
        Document document = DocumentHelper.parseText(xml);
        Element rootElement = document.getRootElement();
        Element ele1 = rootElement.element(element1);
        Element ele2 = ele1.element(element2);
        Element operatorIdElement = ele2.element(element3);
        for (Iterator it = operatorIdElement.elementIterator(element4); it.hasNext();) {
            Element element = (Element) it.next();
            ip = element.elementTextTrim("ftpIP");
            port = element.elementTextTrim("ftpPort");
            username = element.elementTextTrim("username");
            password = element.elementTextTrim("password");
            filepath = element.elementTextTrim("filepath");
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("ip", ip);
            map.put("port", port);
            map.put("username", username);
            map.put("password", password);
            map.put("filepath", filepath);
            ftpList.add(map);
        }
        return ftpList;
    }

}
